<?php

/**
 * The Navigation class.
 *
 */
class Navigation {
	/**
	 * Identifier for this class
	 */
	protected $id;
	/**
	 * First level items
	 */
	protected $items;
	/**
	 * Second level items
	 */
	protected $subitems;
	/**
	 * Selected item (page) from the first level
	 */
	protected $selectedItem;
	/**
	 * Selected item (page) from the first level
	 */	
	protected $selectedSubitem;
	/**
	 * Active page
	 */
	protected $activeItem;
	
	/**
	 * Default constructor
	 */
	public function __construct($id) {
		$this->id 			= $id;	
		$this->items 		= array();
		$this->subitems 	= array();
		$this->activeItem 	= CSEFILE;
	}
	
	public function getId() {
		return $this->id;
	}
	
	/**
	 * Adds a new menu item at the first level. If the parent parameter is specified then the menu item will be considered as subitem and placed at the second level.
	 *
	 * @param string file the target file of the menu item
	 * @param string title the menu item title
	 * @param boolean auth indicates whether or not the user have to be authorized in order to view this menu item
	 * @param string parent the parent file, if this is a subitem
	 */
	public function addItem($file, $title, $auth, $parent=null) {
		$config = Cinnamon::registry('config');
	
		// add item to this instance
		if ( empty($parent) ) {
			$this->items[$file] = array('file'=>$file, 'title'=>$title, 'auth'=>$auth);
			$this->subitems[$file] = array();
		} else {
			$this->subitems[$parent][$file] = array('file'=>$file, 'title'=>$title, 'auth'=>$auth, 'parent'=>$parent);
		}
	}
	
	/** 
	 * Removes an existing menu item from the menu. If the parent paremeter is not specified then the menu item will be considered as item and its subitems will be removed as well.
	 * 
	 * @param string file the target file of the menu item to be removed
	 * @param string parent the parent file, if this is a subitem
	 */	 
	public function removeItem($file, $parent=null) {
		$config = Cinnamon::registry('config');
	
		if ( empty($parent) ) {
			$this->items = array_key_remove($this->items, $file);
			$this->subitems = array_key_remove($this->subitems, $file);
		} else {
			$this->subitems[$parent] = array_key_remove($this->subitems[$parent], $file);
		}
	}
	
	protected function buildSelected() {
		$evt = new Event( get_class($this), 'instance', $this );
		Cinnamon::registry('events')->dispatchEvent($evt);	
	
		$script = $this->activeItem;

		// check in parent items
		$found = false;
		foreach ( $this->items as $key => $item ) {
			if ( $item['file'] == $script ) {
				$this->selectedItem = $key;
				$this->selectedSubitem = key($this->subitems[$key]);
				$found = true;
				break;
			}
		}
		
		// check in sub items
		if ( !$found ) {
			foreach ( $this->subitems as $parent => $subitems ) {
				foreach ( $subitems as $key => $item ) {
					if ( $item['file'] == $script ) {
						$this->selectedItem = $parent;
						$this->selectedSubitem = $key;
					}
				}
			}
		}
	}
	
	public function setActiveItem($item) {
		$this->activeItem = $item;
	}
	
	/**
	 * Renders the specified level of this menu
	 *
	 * @param int level the level to be rendered (1 or 2)
	 */
	public function render($level, $before='<ul>', $after='</ul>', $class='current') {
		if ( $level == 0 ) {
			$this->buildSelected();
		}
		
		$items = ( $level == 0 ) ? $this->items : $this->subitems[$this->selectedItem];
		$selectedItem = ( $level == 0 ) ? $this->selectedItem : $this->selectedSubitem;

		$isAuthenticated = Cinnamon::registry('session')->isAuthenticated();

		if ( count($items) > 0 ) {
			echo $before;
			foreach ( $items as $key => $item ) {
				if ( !$item['auth'] || ( $item['auth'] && $isAuthenticated ) ) {
					$url = CSEURL . $item['file'];
					echo '<li><a href="'. $url .'"';
					if ( $selectedItem == $key ) {
						echo ' class="'. $class .'"';
					}
					echo '>'. $item['title'] .'</a></li>';
				}
			}
			echo $after;
		}
	}
}
?>